package com.chiancloud.shield.tools;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * Created by wanghui on 14-8-20.
 */
public class ShieldUtils {
    private static final Logger log = LoggerFactory.getLogger(ShieldUtils.class);
    public static List<String> collectionToStringList(Collection c) {
        List<String> l = new ArrayList<>(c.size());
        for (Object o : c) {
            l.add(o.toString());
        }
        return l;
    }
    public static String join(Collection collection, String separator) {
        return join(collection, separator, true);
    }
    public static String join(Collection collection, String separator, boolean trailing) {
        StringBuilder b = new StringBuilder();
        for (Object o : collection) {
            b.append(o);
            b.append(separator);
        }
        return trailing?
                b.toString()
                : (b.substring(0, b.length() - separator.length()));
    }

    public static String join(String[] collection, String separator) {
        return join(collection, separator, true);


    }

    public static String join(String[] collection, String separator,
                              boolean trailing) {
        return join(Arrays.asList(collection), separator, trailing);
    }


    public static boolean isUnset(String s) {
        return s == null || s.isEmpty();
    }

    public static boolean isSet(String s) {
        return !isUnset(s);
    }

    public static <T1, T2> Map<T1, T2> mergeMapsIgnoreDuplicateKeys(Map<T1, T2> first,
                                                                    Map<T1, T2> second) {
        for (Map.Entry<T1, T2> entry : second.entrySet()) {
            T1 key = entry.getKey();
            if (!first.containsKey(key)) {
                first.put(key, entry.getValue());
            }
        }
        return first;
    }

}
